using System.Diagnostics;
using DimensionsHelper.LanguageService.Symbols;
using DimensionsHelper.LanguageService.Syntax;

namespace DimensionsHelper.LanguageService;

public class AnalysisSelectCaseStatement : AnalysisStatement
{
    private readonly List<AnalysisSelectCaseClause> _clauses = [];

    internal AnalysisSelectCaseStatement(SelectStatement syntaxNode,
        AnalysisExpression expression,
        AnalysisNode? parent) :
        base(AnalysisBoundKinds.SelectStatement,
            syntaxNode,
            parent)
    {
        Expression = expression;
        Expression.Parent = this;
    }

    public AnalysisExpression Expression { get; }

    public override int ChildrenCount => _clauses.Count + 1;

    public override IEnumerable<AnalysisNode> GetChildren()
    {
        return [Expression, .._clauses];
    }

    internal override void AddChild(AnalysisNode child)
    {
        Debug.Assert(child.BoundKind == AnalysisBoundKinds.SelectCaseClause);
        _clauses.Add((AnalysisSelectCaseClause)child);
        child.Parent = this;
    }
}

public static partial class AnalysisNodeFactory
{
    /// <summary>
    /// 创建一个Select语句
    /// </summary>
    /// <param name="syntaxNode">原始语法节点</param>
    /// <param name="expression">选择表达式</param>
    /// <param name="parent">父节点，可以忽略</param>
    /// <param name="withSymbol">语法节点所在的With语句上下文类型符号</param>
    public static AnalysisSelectCaseStatement SelectCaseStatement(SelectStatement syntaxNode,
        AnalysisExpression expression, AnalysisNode? parent, ITypeSymbol? withSymbol)
    {
        return new AnalysisSelectCaseStatement(syntaxNode, expression, parent).WithContainingWithSymbol(withSymbol);
    }
}